import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../view/login.vue'
import home from '../view/home.vue'
import Welcome from '../view/welcome.vue'
import User from '../component/user.vue'
import Rights from '../component/power/rights.vue'
import Roles from '../component/power/roles.vue'
import Cate from '../component/goods/cate.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: User },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to代表将要访问的路径
  // from 代表从哪个路径访问
  // next表示放行括号里加路径表示强行跳转
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
